(function(undefined){
	var coord_obj = {};
	coord_obj.show=function(){

		var script = document.createElement("script");
		script.src = "http://api.map.baidu.com/api?v=1.2&callback=coord_obj.initpoint";
		document.body.appendChild(script);
	}

	coord_obj.initpoint=function(){
		coord_obj.map = {};
		coord_obj.map.coordInput = $("input[name=coord]");
		coord_obj.map.addressInput = $("#address");
		initPoint = null;

		var myGeo = new BMap.Geocoder();
		address1 = $("#city_pro").find("option:selected").text()+$("#city_city").find("option:selected").text()+ $("#city_dis").find("option:selected").text(); //不带地址的城市地区
		address = $("#city_pro").find("option:selected").text() + $("#city_city").find("option:selected").text() +$("#city_dis").find("option:selected").text()+ coord_obj.map.addressInput.val();
		myGeo.getPoint(address, function(point){

			if(!point){

				//去掉地址再次请求
				myGeo.getPoint(address1, function(point){
					coord_obj.initialize(point);
				}, "");
				return false;
			}
			coord_obj.initialize(point);
		}, "");
	}
	coord_obj.initialize=function(initPoint) {

		//显示地图
		$("#coord_iframe_tr").show();
		coord_obj.map.obj = new BMap.Map('coord_iframe');   //地图容器层
		if(!initPoint){
			initPoint = new BMap.Point(106.491, 29.303);    //创建默认中心点坐标
			coord_obj.map.obj.centerAndZoom(initPoint, 6); //设置中心点坐标和地图级别
		}else{
			coord_obj.map.obj.centerAndZoom(initPoint, 12); //设置中心点坐标和地图级别
		}

		//添加标注
		marker = new BMap.Marker(initPoint);    //创建标注
		coord_obj.map.obj.addOverlay(marker);
		marker.enableDragging();    //标注可拖拽
		marker.addEventListener("dragend", function(e){
			if(confirm("当前位置：" + e.point.lng + ", " + e.point.lat + "\r\n是否修改坐标？")){
				$("input[name=coord]").val(e.point.lng + ", " + e.point.lat);
			}
			coord_obj.map.obj.panTo(new BMap.Point(e.point.lng, e.point.lat));
		})
		marker.addEventListener("dragging", function(e){
			tmp_left = e.target.domElement.offsetLeft + 28;
			tmp_top  = e.target.domElement.offsetTop;
			coord_obj.map.div.css({
				left: tmp_left + "px",
				top: tmp_top + "px"
			})
			coord_obj.map.div.text(e.point.lng + "," + e.point.lat)
		})

		coord_obj.map.obj.addEventListener("click", function(e){
			marker.setPosition(new BMap.Point(e.point.lng, e.point.lat));
			coord_obj.map.obj.panTo(new BMap.Point(e.point.lng, e.point.lat));
			if(confirm("当前位置：" + e.point.lng + ", " + e.point.lat + "\r\n是否修改坐标？")){
				$("input[name=coord]").val(e.point.lng + "," + e.point.lat);
			}
		})
		coord_obj.map.obj.addControl(new BMap.NavigationControl());
		coord_obj.map.obj.enableScrollWheelZoom();

		div = document.createElement("div");
		div.style.position = "absolute";
		// div.style.background = "#FFFFFF";
		div.style.color = "#FF0000";
		coord_obj.map.obj.getPanes().markerPane.appendChild(div);
		coord_obj.map.div = $(div);
	}


	if(window.coord_obj == undefined) {
		window.coord_obj = coord_obj;
	}
})();